<!--
 * @Author: your name
 * @Date: 2022-05-04 16:30:40
 * @LastEditTime: 2022-05-04 16:33:31
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \算法测试\day0906\1823找出游戏的获胜者.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var findTheWinner = function (n, k) {
            let arr = new Array(n + 1).fill(true);
            let ans = n, i = 0, num = 1;
            while (true) {
                if (!arr[num]) {
                    if (num === n) { //防止越界
                        num = 1;
                        continue;
                    }
                    num++;
                    continue;
                }
                i++;
                if (i === k) {
                    if (ans === 1) {
                        return num;
                    } else {
                        i = 0;
                        arr[num] = false;
                        ans--;
                    }
                }
                if (num === n) {//防止越界
                    num = 1;
                    continue;
                }
                num++;
            }
        };
        console.log(findTheWinner(5, 2));
    </script>
</body>

</html>